<?php

namespace app\job\swoole\timer;

use Cron\CronExpression;
use Swoole\Process;
use swoole\Timer;
use think\swoole\Manager;

class TimerHandler
{
    public function __construct(Manager $manager, Process\Pool $pool)
    {
        $config = $manager->getConfig('timer.task', []);
        dump("定时器启动...");
        foreach ($config as $row) {
            //每分钟执行一次
            Timer::tick(60 * 1000, function () use ($row){
                $cron = new CronExpression($row['cron']);
                dump( $cron->isDue(), date('Y-m-d H:i:s'));
                if ($cron->isDue()) {
                    dump("success");
                }
            });
        }

    }


    // 注册定时任务
    public function registerCronTasks()
    {
        /**
         * 解析器
         * https://packagist.org/packages/dragonmantank/cron-expression
         */
        Timer::tick(1000, function (){
            dump("当前时间是：".date('Y-m-d H:i:s'));
        });

    }

}